package com.blockmeta.bbs.immodule.adapter;

import android.content.Context;

import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.widget.msgview.IMsgView;
import com.blockmeta.bbs.immodule.widget.msgview.IMsgViewManger;
import com.blockmeta.bbs.immodule.widget.msgview.MsgViewManager;
import com.blockmeta.bbs.immodule.widget.msgview.SimpleMsgView;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by author on 2019/3/25
 */
public class ChatAdapter extends BaseMultiItemQuickAdapter<ChatMessage, BaseViewHolder> {

    private Context mContext;
    private int mMaxDate = 0;
    private int mCurrentDate;
    private IMsgViewManger iMsgViewManger;


    public ChatAdapter(Context context, List<ChatMessage> data) {
        super(data);
        this.mContext = context;
        iMsgViewManger = new MsgViewManager<ChatMessage>(mContext);
        iMsgViewManger.addItemTypes(this);

    }


    @Override
    protected void convert(BaseViewHolder helper, ChatMessage item) {
        int itemViewType = helper.getItemViewType();
        IMsgView<ChatMessage> msgView = iMsgViewManger.getMsgView(itemViewType);
        if(msgView != null){
            //对不同的view 获取数据进行填充
            if(msgView instanceof SimpleMsgView){
                msgView.convert(helper, item);
            }else {
                int layoutPosition = helper.getLayoutPosition();
                if (layoutPosition > 0) {
                    ChatMessage lastMsg = getData().get(layoutPosition - 1);
                    msgView.convert(helper, item, lastMsg);
                }else {
                    msgView.convert(helper, item, null);
                }
            }
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (getData().size() != 0) {
            getData().get(position).setTAG_WHERE(ChatMessage.IM_CHATDETAIL_TAG);
        }
        return super.getItemViewType(position);
    }




}
